package com.hdoit.ares.web.acc.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.acc.model.AccountDetail;
import com.hdoit.ares.logical.acc.service.IAccountDetailService;
import com.hdoit.ares.logical.viewmodel.acc.FindAccountDetail;
import com.tennetcn.component.webapi.BaseApi;
import com.tennetcn.component.webapi.ResponseModel;
import com.tennetcn.component.webapi.ResponseStatus;

@Controller
@RequestMapping(value="/api/v1/accountDetailAres/",method = {RequestMethod.GET,RequestMethod.POST},produces="application/json;charset=utf-8")
@Api(value="/api/v1/accountDetailAres/")
@CrossOrigin(origins = "*",maxAge = 3600)
public class AccountDetailAresApi extends BaseApi{
	
	private IAccountDetailService accountDetailService;
	public IAccountDetailService getAccountDetailService() {
		return accountDetailService;
	}
	@Resource
	public void setAccountDetailService(IAccountDetailService accountDetailService) {
		this.accountDetailService = accountDetailService;
	}
	
	private final String balanceListApi="balanceList";
	private final String balanceApi="balance";
	
	    // 按条件查询余额明细
	    @ResponseBody
	    @RequestMapping(value = balanceListApi)
	    @ApiOperation(notes = balanceListApi, httpMethod = "POST", value = "获取余额详情列表信息")
		public String balanceDetailList(FindAccountDetail findAccountDetail,
				@ApiParam("第几页,必选") @RequestParam(value="pageNum",required=true)Integer pageNum,
				@ApiParam("每页数量") @RequestParam(value="length",required=true)Integer length
				/*@ApiParam("查询的开始时间") @RequestParam(value="strDate")String strDate,
				@ApiParam("查询的结束时间") @RequestParam(value="endDate")String endDate*/) {
			
	    	/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
		    try {
		    	if(!StringUtils.isEmpty(strDate)){
		    		findAccountDetail.setStartDate(sdf.parse(strDate));
		    	}
		    	if(!StringUtils.isEmpty(endDate)){
		    		findAccountDetail.setEndDate(sdf.parse(endDate));
		    	}
			} catch (ParseException e) {
				e.printStackTrace();
			}*/
		    
	    	ResponseModel rm=new ResponseModel();
	    	int start=(pageNum-1)*length;
	    	String custId=getLoginId();
			List<AccountDetail> accountDetails = accountDetailService.queryAccountList(findAccountDetail, custId,start,length);
			
			if(accountDetails.size()>0){
				rm.setStatus(ResponseStatus.success);
				rm.setMessage("ok");
				List<String> params = new ArrayList<String>();
				Field[] fields = AccountDetail.class.getDeclaredFields();
				for(Field field : fields){
					params.add(field.getName());
				}
				
				rm.putList("accountDetail", accountDetails, params);
			}else{
				rm.setMessage("没有查询到数据");
			}

			Double incomeMoney = null;
			AccountDetail accountDetail1 = accountDetailService.querySumIncomeMoney(findAccountDetail, custId);
			if (accountDetail1 == null) {
				incomeMoney = 0.00;
			} else {
				incomeMoney = accountDetail1.getIncomeMoney();
			}
			if(incomeMoney==null){
				rm.setMessage("总收入计算错误");
			}else{
			    rm.put("incomeMoney", incomeMoney);
			}

			Double expendMoney = null;
			AccountDetail accountDetail2 = accountDetailService.querySumExpendMoney(findAccountDetail, custId);
			if (accountDetail2 == null) {
				expendMoney = 0.00;
			} else {
				expendMoney = accountDetail2.getExpendMoney();
			}
			if(incomeMoney==null){
				rm.setMessage("总支出计算错误");
			}else{
				rm.put("expendMoney", expendMoney);
			}

			return rm.toJson();
		}
	    
	    @ResponseBody
	    @RequestMapping(value = balanceApi)
	    @ApiOperation(notes = balanceApi, httpMethod = "POST", value = "选择的余额详情页面")
	    public String balance(String id) {

	    	ResponseModel rm=new ResponseModel();
	    	if(StringUtils.isEmpty(id)){
				rm.setMessage("id为空，请输入id");
			}
	    	
			AccountDetail accountDetail = accountDetailService.queryAccount(id);
			rm.put("accountDetail", accountDetail);

			return rm.toJson();
		}
}
